package cn.tedu.csmall.product.web;

import cn.tedu.csmall.product.ex.ServiceException;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;

@Data
public class JsonResult<T> implements Serializable {
    @ApiModelProperty("状态吗")
    private Integer state;
    @ApiModelProperty("操作失败相应的文本")
    private String message;
    @ApiModelProperty("操作成功相应的数据")
    private T data;

    public JsonResult() {
    }

    public JsonResult(Integer state, String message) {
        this.state = state;
        this.message = message;
    }



    public static  JsonResult <Void> ok (){
        return ok(null);
    }

    public static <T> JsonResult ok (T data){
        JsonResult<T> jsonResult = new JsonResult<T>();
        jsonResult.data=data;
        jsonResult.state=ServiceCode.OK.getValue();
        return jsonResult;
    }

    public static  JsonResult <Void> fail(ServiceException e){
        return fail(e.getServiceCode(), e.getMessage());
    }

    public static  JsonResult <Void> fail (ServiceCode serviceCode ,String message){
        JsonResult<Void> jsonResult = new JsonResult<>();
        jsonResult.state=serviceCode.getValue();
        jsonResult.message=message;
        return jsonResult;
    }
}
